package cn.icanci.learn.v1.springaop;

import java.lang.reflect.Method;

import org.springframework.aop.ClassFilter;
import org.springframework.aop.MethodMatcher;
import org.springframework.aop.Pointcut;

/**
 * @author icanci
 * @date Created in 2021/06/27 23:38
 * @version 1.0
 */
public class MyPointcut implements Pointcut {
    /**
     * Return the ClassFilter for this pointcut.
     * @return the ClassFilter (never {@code null})
     * Pointcut 持有类过滤器 类过滤器的作用就是判断某个类 是否符合切点位置
     */
    @Override
    public ClassFilter getClassFilter() {
        return new ClassFilter() {
            @Override
            public boolean matches(Class<?> clazz) {
                return true;
            }
        };
    }

    /**
     * Return the MethodMatcher for this pointcut.
     * @return the MethodMatcher (never {@code null})
     * Pointcut 持有方法匹配器，方法匹配器的作用就是 判断 类中的某个方法，匹配条件的方法才会被增强
     */
    @Override
    public MethodMatcher getMethodMatcher() {
        return new MethodMatcher() {
            @Override
            public boolean matches(Method method, Class<?> targetClass) {
                if (method.getName().equals("eat")) {
                    return true;
                }
                return false;
            }

            @Override
            public boolean isRuntime() {
                return false;
            }

            @Override
            public boolean matches(Method method, Class<?> targetClass, Object... args) {
                return false;
            }
        };
    }
}
